home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / mus / play / SPLibDev.lha / superplay-lib_DEV / Programmers / Example_Tools / SimplePlay / SimplePlay.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-15  |  11.9 KB  |  392 lines

  1. /* ======================================================================== */
  2. /* = Programmname    : SimplePlay V7.2                                    = */
  3. /* =                                                                      = */
  4. /* ======================================================================== */
  5. /* = Author/Copyright : (c) 1994-97 by Andreas Ralph Kleinert.            = */
  6. /* =                   Freeware. All rights reserved.                     = */
  7. /* =                                                                      = */
  8. /* =                   Use it as an example for programming               = */
  9. /* =                   superplay.library !                                = */
  10. /* =                                                                      = */
  11. /* ======================================================================== */
  12. /* = Function         : SPObject operations :                             = */
  13. /* =                    - Play Samples/Modules (all SPObjects)            = */
  14. /* ======================================================================== */
  15. /* = Last Update      : 15.8.1997                                          = */
  16. /* =                                                                      = */
  17. /* ======================================================================== */
  18. /* = Remarks          : Needs "asl.library" V37+                          = */
  19. /* =                    and "superplay.library" V7+.                      = */
  20. /* =                                                                      = */
  21. /* ======================================================================== */
  22. /* = Compiler         : SAS/C V6.58                                       = */
  23. /* =                    (smakefile)                                       = */
  24. /* ======================================================================== */
  25.  
  26. #define __USE_SYSBASE
  27.  
  28. #include <intuition/intuitionbase.h>
  29.  
  30. #include <superplay/superplay.h>
  31.  
  32. #include <libraries/asl.h>
  33. #include <workbench/startup.h>
  34. #include <exec/interrupts.h>
  35.  
  36. #include <proto/exec.h>
  37. #include <proto/dos.h>
  38. #include <proto/intuition.h>
  39. #include <proto/graphics.h>
  40. #include <proto/asl.h>
  41. #include <proto/superplay.h>
  42.  
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46.  
  47.  
  48.    /* Help- and Info- Texts */
  49.  
  50. char entry1_text  [] = "\2331;32;40mSimplePlay V7.2 \2330;32;40m\2333;32;40m(FREEWARE)\2330;32;40m\n(c) 1994-97 by Andreas Ralph Kleinert.\nAndreas R. Kleinert, Sandstrasse 1, D-57072 Siegen, Germany.\n";
  51. char entry2_text  [] = "Plays Sound Samples and Modules via superplay.library.\n";
  52. char entry3_text  [] = "USAGE : \2330;33;40mSimplePlay\2330;31;40m [? | -STOP | -REMOVE | <Sample/ModuleFileName>]\n";
  53.  
  54.  
  55. char ver_text [] = "\0$VER: SimplePlay 7.2 (15.8.97)";
  56.  
  57.  
  58. /* *************************************************** */
  59. /* *                                                 * */
  60. /* * Error-Messages for Leave() and KF_Message()     * */
  61. /* *                                                 * */
  62. /* *************************************************** */
  63.  
  64. char intuitionlib_text  [] = "You need \42intuition.library\42 V37+ !";
  65. char asllib_text        [] = "You need \42asl.library\42 V37+ !";
  66. char splib_text         [] = "You need \42superplay.library\42 V7+ !";
  67.  
  68.  
  69. /* *************************************************** */
  70. /* *                                                 * */
  71. /* * MACROs for Version-Tests                        * */
  72. /* *                                                 * */
  73. /* *************************************************** */
  74.  
  75. #define LibVer(x) ( ((struct Library *) x)->lib_Version )
  76. #define OS_VER    LibVer(SysBase)
  77.  
  78.  
  79. /* *************************************************** */
  80. /* *                                                 * */
  81. /* * Function Declarations                           * */
  82. /* *                                                 * */
  83. /* *************************************************** */
  84.  
  85. void __regargs SP_Play(char *filename);
  86. void __regargs SP_Remove(void);
  87.  
  88. void __regargs Leave(char *endtext, long code);
  89.  
  90.  
  91.    /* Functions from module "SimplePlay_Subs.o" : */
  92.  
  93. extern void __stdargs SP_Printf(char *formatstring, ...);
  94.  
  95.  
  96. /* *************************************************** */
  97. /* *                                                 * */
  98. /* * Additional Base Declarations                    * */
  99. /* *                                                 * */
  100. /* *************************************************** */
  101.  
  102. extern struct ExecBase *SysBase;
  103.  
  104. struct IntuitionBase *IntuitionBase = N;
  105. struct SuperPlayBase *SuperPlayBase = N;
  106. struct Library       *AslBase       = N;
  107.  
  108.  
  109. /* *************************************************** */
  110. /* *                                                 * */
  111. /* * Variables for Port Usage                        * */
  112. /* *                                                 * */
  113. /* *************************************************** */
  114.  
  115. char portname   [] = "SimplePlay.port";
  116. char portname_2 [] = "SimplePlay_2.port";
  117.  
  118. struct MsgPort *port_1 = N;
  119. struct MsgPort *port_2 = N;
  120.  
  121.  
  122. /* *************************************************** */
  123. /* *                                                 * */
  124. /* * Structure-Definitions and Flags                 * */
  125. /* *                                                 * */
  126. /* *************************************************** */
  127.  
  128. struct SPMessage
  129. {
  130.  struct Message sp_Msg;  /* The Message itself           */
  131.  ULONG          sp_Flag; /* (See flag-definitions below) */
  132.  APTR           sp_Ext;  /* Future expansions            */
  133. };
  134.  
  135. #define SPF_REMOVE ((ULONG) 1)
  136. #define SPF_STOP   ((ULONG) 2)
  137. #define SPF_CONT   ((ULONG) 3)
  138.  
  139.  
  140. /* *************************************************** */
  141. /* *                                                 * */
  142. /* * Globally Accessed Data                          * */
  143. /* *                                                 * */
  144. /* *************************************************** */
  145.  
  146. struct TextAttr __aligned topazattr = { (STRPTR) "topaz.font", TOPAZ_EIGHTY, FS_NORMAL, FPF_ROMFONT };
  147.  
  148. struct SPMessage __aligned our_msg =
  149. {
  150.  { N, N, N, NT_MESSAGE, 0, N },
  151.  N,
  152.  N
  153. };
  154.  
  155.  
  156. /* *************************************************** */
  157. /* *                                                 * */
  158. /* * MAIN                                            * */
  159. /* *                                                 * */
  160. /* *************************************************** */
  161.  
  162. extern struct WBStartup *WBenchMsg;
  163. char                    *wbloadname = N;
  164. long                     wb         = FALSE;
  165.  
  166. long  __stack        = 4096;
  167. char *__procname     = "SimplePlay V7.1";
  168. long  __priority     = 1;
  169. long  __BackGroundIO = TRUE;
  170.  
  171. extern BPTR _Backstdout;
  172.  
  173. void main(long argc, char **argv)
  174. {
  175.  port_1 = FindPort(portname);
  176.  
  177.  if(argc==0)
  178.   {
  179.    wb = TRUE;
  180.  
  181.    if(WBenchMsg->sm_NumArgs>1) wbloadname = WBenchMsg->sm_ArgList[1].wa_Name;
  182.   }
  183.  
  184.  if( argc > 2 || (argv[1][0] =='?') )
  185.   {
  186.    SP_Printf("%s%s%s", entry1_text, entry2_text, entry3_text);
  187.  
  188.    Leave(N, 0);
  189.   }
  190.  
  191.  IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 37);
  192.  if(!IntuitionBase) Leave(intuitionlib_text, 100);
  193.  
  194.  AslBase = (struct Library *) OpenLibrary("asl.library", 37);
  195.  if(!AslBase) Leave(asllib_text, 102);
  196.  
  197.  SuperPlayBase = (struct SuperPlayBase *) OpenLibrary("superplay.library", 7);
  198.  if(!SuperPlayBase) Leave(splib_text, 102);
  199.  
  200.  
  201.  /* Play */
  202.  
  203.  if(argc < 2 && !wbloadname)
  204.   {
  205.    struct FileRequester     *request;
  206.    struct TagItem __aligned  tags[4];
  207.    char                      namebuffer [256];
  208.  
  209.    namebuffer[0] = (char) 0;
  210.  
  211.    tags[0].ti_Tag  = (Tag)   ASL_Hail;
  212.    tags[0].ti_Data = (ULONG) "Select Sample/Module to play :";
  213.  
  214.    tags[1].ti_Tag  = (Tag)   ASL_OKText;
  215.    tags[1].ti_Data = (ULONG) " Play ";
  216.  
  217.    tags[2].ti_Tag  = (Tag)   ASL_CancelText;
  218.    tags[2].ti_Data = (ULONG) " Quit ";
  219.  
  220.    tags[3].ti_Tag  = (Tag)   TAG_DONE;
  221.    tags[3].ti_Data = (ULONG) N;
  222.  
  223.    request = AllocAslRequest(ASL_FileRequest, N);
  224.    if(request)
  225.     {
  226.      if(AslRequest(request, &tags[0]))
  227.       {
  228.        ULONG len;
  229.  
  230.        strcpy(namebuffer, request->rf_Dir);
  231.  
  232.        len = strlen(namebuffer);
  233.        if(len)
  234.         {
  235.          if(    (namebuffer[len-1] !=        ':')
  236.              && (namebuffer[len-1] !=        '/')
  237.              && (namebuffer[0]     != (char) 0) ) strcat(namebuffer, "/");
  238.         }
  239.  
  240.        strcat(namebuffer, request->rf_File);
  241.  
  242.        if(namebuffer[0] != (char) 0) SP_Play(namebuffer);
  243.       }
  244.  
  245.      FreeAslRequest(request);
  246.     }
  247.   }else
  248.   {
  249.    if(wbloadname) SP_Play(wbloadname);
  250.     else
  251.      {
  252.       if(argv[1][0]!=(char) '-') SP_Play(argv[1]);
  253.        else
  254.         {
  255.          if(!stricmp(argv[1], "-STOP"))
  256.           {
  257.            if(port_1)
  258.             {
  259.              port_2 = CreatePort(portname_2, 0);
  260.  
  261.              our_msg.sp_Msg.mn_ReplyPort = port_2;
  262.              our_msg.sp_Msg.mn_Length    = sizeof(struct SPMessage);
  263.  
  264.              our_msg.sp_Flag             = SPF_STOP;
  265.              our_msg.sp_Ext              = N;
  266.  
  267.              PutMsg(port_1, (struct Message *) &our_msg);
  268.  
  269.              WaitPort(port_2);
  270.              GetMsg(port_2);
  271.  
  272.              DeletePort(port_2);
  273.             }
  274.           }
  275.  
  276.          if(!stricmp(argv[1], "-REMOVE")) SP_Remove();
  277.         }
  278.      }
  279.   }
  280.  
  281.  Leave(N, 0);
  282. }
  283.  
  284.  
  285. /* *************************************************** */
  286. /* *                                                 * */
  287. /* * LEAVE : Global Exit Function Replacement        * */
  288. /* *                                                 * */
  289. /* *************************************************** */
  290.  
  291. void __regargs Leave(char *endtext, long code)
  292. {
  293.  if(SuperPlayBase) CloseLibrary((APTR) SuperPlayBase);
  294.  if(AslBase)       CloseLibrary((APTR) AslBase);
  295.  if(IntuitionBase) CloseLibrary((APTR) IntuitionBase);
  296.  
  297.  if(endtext)       if(!wb) SP_Printf("%s\n", endtext);
  298.  
  299.  exit(code);
  300. }
  301.  
  302. /* *************************************************** */
  303. /* *                                                 * */
  304. /* * Play-Function                                   * */
  305. /* *                                                 * */
  306. /* *************************************************** */
  307.  
  308. void __regargs SP_Play(char *filename)
  309. {
  310.  APTR handle;
  311.  ULONG retval = SPERR_NO_ERROR;
  312.  struct SPMessage *got_msg      = N;
  313.  struct MsgPort   *our_waitport = N;
  314.  ULONG run = TRUE, flag;
  315.  
  316.  SP_Remove();
  317.  
  318.  our_waitport = CreatePort(portname, 0);
  319.  if(our_waitport)
  320.   {
  321.    handle = SPL_AllocHandle(N);
  322.    if(handle)
  323.     {
  324.      if(!(retval = SPL_InitHandleAsDOS(handle, N)))
  325.       {
  326.        if(!(retval = SPL_SuperPlay(handle, filename)))
  327.         {
  328.          for(;run;)
  329.           {
  330.            WaitPort(our_waitport);
  331.  
  332.            got_msg = (APTR) GetMsg(our_waitport);
  333.            flag = got_msg->sp_Flag;
  334.  
  335.            switch(flag)
  336.             {
  337.              case SPF_REMOVE : { SPL_FreeHandle(handle); run = FALSE; break; }
  338.              case SPF_STOP   : { SPL_StopReplay(handle);              break; }
  339.             }
  340.  
  341.            ReplyMsg((APTR) got_msg);
  342.           }
  343.         }
  344.       }
  345.  
  346.     }else retval = SPERR_NO_HANDLE;
  347.  
  348.    while( got_msg = (APTR)GetMsg(our_waitport) ) ReplyMsg((APTR)got_msg);
  349.  
  350.    DeletePort(our_waitport);
  351.   }
  352.  
  353.  if(retval) Leave(SPL_GetErrorString(retval), 0);
  354. }
  355.  
  356. /* *************************************************** */
  357. /* *                                                 * */
  358. /* * Remove-Function                                 * */
  359. /* *                                                 * */
  360. /* *************************************************** */
  361.  
  362. void __regargs SP_Remove(void)
  363. {
  364.  struct MsgPort *stfr_port = N;
  365.  
  366.  port_1 = FindPort(portname); /* still there ? */
  367.  if(port_1) /* remove previous song and task, if existant */
  368.   {
  369.    stfr_port = CreatePort(portname_2, 0);
  370.    if(stfr_port)
  371.     {
  372.      our_msg.sp_Msg.mn_ReplyPort = stfr_port;
  373.      our_msg.sp_Msg.mn_Length    = sizeof(struct SPMessage);
  374.  
  375.      our_msg.sp_Flag             = SPF_REMOVE;
  376.      our_msg.sp_Ext              = N;
  377.  
  378.  
  379.      port_1 = FindPort(portname); /* still there ? */
  380.      if(port_1)
  381.       {
  382.        PutMsg(port_1, (struct Message *) &our_msg);
  383.        WaitPort(stfr_port);
  384.       }
  385.  
  386.      while( GetMsg(stfr_port) ) ;
  387.  
  388.      DeletePort(stfr_port);
  389.     }
  390.   }
  391. }
  392.